const { request }  = require('../../request/index.js');
const ImageSource=  require('../../utils/image_path.js');
const crypto = require("../../utils/test");
const updateData=require('../../request/dataGet.js')

const app=getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        NavbarHeight:app.globalData.navBarHeight + app.globalData.statusBarHeight,
        WindowWidth:app.globalData.windowWidth,
        ScreenHeight:app.globalData.ScreenHeight,
        navigationSetting:{
            title:'设置',
            model:1,
        },
        pageType:1,
        ruleIsActive:[],
        orderData:[],
        imageData:[],
        roleData:[],
        dateData:[],
        priceData:["29.90","59.90","19.90"],
        nowTime:app.globalData.nowTime.substring(0,19).replace('T'," "),
        nowDate:app.globalData.nowTime.substring(0,10),
        category:["生椰拿铁","经典美式","经典拿铁","樱花拿铁","抹茶拿铁","摩卡"],
        listData:[],
        allData:[],
        dateCoupleData:[],
        OrderIsNull:0,
        CoupleIsNull:0,

    },
    onLoad(option){
        this.setData({
            pageType:option.type,
        })
        if(this.data.pageType==0){
            this.setData({
                navigationSetting:{
                    title:'订单',
                    model:1,
                },
            })
        }else{
            this.setData({
                navigationSetting:{
                    title:'优惠券',
                    model:1,
                },
            })
        }
        this.dealOrderData();
        this.dealCouplesData();
    },
    ruleClickItemTap(e){
       console.log(e.currentTarget.dataset.index);
       const index=e.currentTarget.dataset.index;
       var res="ruleIsActive["+index+"]"
       this.setData({
           [res]:!this.data.ruleIsActive[index]
       })
        console.log(this.data.ruleIsActive);
    },
    returnPage(){
        wx.navigateBack({
            delta: 1
        });
    },
    dealOrderData(){
        var that=this;
        var userOrders=wx.getStorageSync("userOrders");
        console.log(userOrders)
        var listData=[]
        var fileList=[]
        var dateList=[]
        if(userOrders.data.length==0){
            that.setData({
                OrderIsNull:1,
            })
        }else{
            that.setData({
                OrderIsNull:0,
            })
        }
        for(var i=0;i<userOrders.data.length;i++){
            var list=userOrders.data[i].message.split('@')
            if(list[0]=="0"){
                fileList.push(ImageSource.Base+'/icon/pageDiy/cup/'+list[1])
            }else if(list[0]=="1"){
                fileList.push(ImageSource.Base+'/icon/pageDiy/mug/'+list[1])
            }else {
                fileList.push(ImageSource.Base+'/icon/pageDiy/pad/'+list[1])
            }
            fileList.push(ImageSource.Base+'/icon/pageDiy/role/'+list[2])
            listData.push(list)
            dateList.push(userOrders.data[i].date)
            console.log(userOrders.data[i])
        }
        wx.cloud.getTempFileURL({
            fileList:fileList,
            success:trees=>{
               var count=0;
               var image=[];
               var role=[];
               for(var i=0;i<userOrders.data.length;i++){
                   image.push(trees.fileList[count++].tempFileURL);
                   role.push(trees.fileList[count++].tempFileURL);
               }
               that.setData({
                   orderData:listData,
                   imageData:image,
                   roleData:role,
                   dateData:dateList
               })
                console.log("asddsad")
                console.log(that.data.dateData)
            }
        })
    },
    dealCouplesData(){
        var userCouples=wx.getStorageSync("userCouples")
        var listData=[]
        var list=[]
        var unusedData=[]
        for(var i=0;i<userCouples.data.length;i++){
            if(userCouples.data[i].message.substring(userCouples.data[i].message.length-6)!="unused")continue;
            var listS=userCouples.data[i].message.split('@')
            list.push(listS[0])
            listData.push(userCouples.data[i].date)
            unusedData.push(userCouples.data[i])
        }
        if(list.length==0){
            this.setData({
                CoupleIsNull:1,
            })
        }else{
            this.setData({
                CoupleIsNull:0,
            })
        }
        console.log("------")
        this.setData({
            dateCoupleData:listData,
            listData:list,
            allData:unusedData,
        })
    },
    userCoupleItemTap(e){
        console.log(this.data.allData)
        console.log(e)
        const index=e.currentTarget.dataset.index
        var that=this;
        wx.showModal({
            title:"您花4.8折喝了一杯非常好喝的"+that.data.category[parseInt(index)],
            duration:1500
        })
        let params={
            url:'/useCouple',
            method: 'PUT',
            header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
            },
            data: {
                id:this.data.allData[index].id
            },
        }
        request(params).then(()=>{
            updateData.getCouples().then(()=>{
                this.dealCouplesData()
            })

        })
    },
    getRoadMessage(){
        wx.showModal({
            title:"您的订单已到达北京，预计明天到达",
            duration:1500
        })
    },
    getMoney(){
        wx.showModal({
            title:"成功退款",
            duration:1500
        })
    }
})